import javax.microedition.lcdui.*;

/**
 *
 * @author  Administrator
 * @version
 */
public class GraphixScreen extends Canvas implements CommandListener {
    CellularBASIC parent = null; // handle to parent MIDlet
    Command menuCommand, entrCommand; // on-screen command
    int width = 0, height = 0; // withd/height of screen
    boolean firstTime = false; 
    Image offscreen = null; // double buffering of graphics
    StringBuffer text = null; // double buffering of text
    
    /**
     * Create new GraphixScreen
     */
    public GraphixScreen(CellularBASIC parentMIDlet) {
        parent = parentMIDlet;
        initialize();
    }
    
    public GraphixScreen() {
    }
    
    /** initialize all components */
    public void initialize() {
        text = new StringBuffer(); // new text buffer
        menuCommand = new Command("Menu", Command.CANCEL, 1);
        entrCommand = new Command("Enter", Command.BACK, 1);
        width = getWidth();
        height = getHeight();
        offscreen = Image.createImage(width, height); // new graphix buffer
        try {
            // Set up this canvas to listen to command events
            setCommandListener(this);
            addCommand(entrCommand);
            addCommand(menuCommand);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * paint
     */
    public void paint(Graphics g) {
        Graphics saved = g;
        // get the offscreen graphix handle
        if (offscreen != null) { g = offscreen.getGraphics(); }
        saved.drawImage(offscreen, 0, 0, 0); // draw on both offscreen and actual screen
    }
    
    /**
     * Called when a key is pressed.
     */
    protected  void keyPressed(int keyCode) {
        String tt = "01234567890";
        //int ch = keyCode - 48;
        //text.append(tt.charAt(ch));
        text.append(String.valueOf((byte)keyCode));
        WorkSpace.printG(text.charAt(text.length() - 1) + "");
    }
        
    /** draw two orthogonal lines  */
    public void axis() {

        Graphics g = offscreen.getGraphics();
        g.setColor(WorkSpace.color);
        g.drawLine(width/2, 0, width/2, height);
        g.drawLine(0, height/2, width, height/2);
        repaint();
        serviceRepaints();
    }
    
    /** clear screen by filling a black rectangle */
    public void cls() {
        Graphics g = offscreen.getGraphics();
        g.setColor(0, 0, 0);
        g.fillRect(0, 0, width, height);
        repaint();
        serviceRepaints();
    }
    
    /**
     * Called when a key is released.
     */
    protected  void keyReleased(int keyCode) {
    }
    
    /**
     * Called when a key is repeated (held down).
     */
    protected  void keyRepeated(int keyCode) {
    }
    
    /**
     * Called when the pointer is dragged.
     */
    protected  void pointerDragged(int x, int y) {
    }
    
    /**
     * Called when the pointer is pressed.
     */
    protected  void pointerPressed(int x, int y) {
    }
    
    /**
     * Called when the pointer is released.
     */
    protected  void pointerReleased(int x, int y) {
    }
    
    /**
     * Called when action should be handled
     */
    public void commandAction(Command command, Displayable displayable) {
        if (command == menuCommand) {
            parent.display.setCurrent(parent.get_menuScreen());
        } else if (command == entrCommand) {
            // insert text into in-bar of outForm
            WorkSpace.inTextField.setString(text.toString());
            text = new StringBuffer();
            // input is ready
            WorkSpace.inReady = true;
        }
    }
}
